<template>
  <div class="produceFactoryDialog">
    <div class="panelWrap">
      <div class="title flex flexac flexjcsb fs14 cfff fwb pl p-r-20">
        <div>添加</div>
        <i class="el-icon-close" @click="handleCloseDialogUpdate" />
      </div>
      <div class="con">
        <div class="con-row">
          <div class="inputLable inputLable1 flex flexac">
            <span class="name"><span class="mustval">*</span>小区名称：</span>
            <div class="flex flexdc">
              <el-input v-model="data.name" class="input" placeholder="请输入小区名称..." />
            </div>
          </div>
        </div>

        <div class="con-row">
          <div class="inputLable inputLable1 flex flexac">
            <span class="name">地产品牌名称：</span>
            <div class="pre">
              <el-input v-model="data.landBrandName" class="input" placeholder="请输入地产品牌名称..." />
              <el-button v-if="data.id !== 0" type="warning" class="selBtn abs" @click="handleSelect">选择</el-button>
            </div>
          </div>
        </div>
        <div class="con-row">
          <div class="inputLable inputLable1 flex flexac">
            <span class="name">备注：</span>
            <el-input v-model="data.remark" class="input" placeholder="请输入备注信息..." />
          </div>
        </div>
      </div>

      <div class="flex flextc footerBtn m-t-35">
        <el-button v-if="!isEdit" class="defaultbtn saveBtn" @click="handleSave">保存</el-button>
        <el-button v-else class="defaultbtn saveBtn" @click="handleUpdate">更新</el-button>
        <el-button v-if="!isEdit" class="defaultbtn clsoeBtn" @click="handleCloseDialog">关闭</el-button>
        <el-button v-else class="defaultbtn clsoeBtn" @click="handleCloseDialogUpdate">关闭</el-button>
      </div>
    </div>
    <CoSelect v-model="coSelectVisible" v-bind="factorytable" :params="params" :tablehead="tablehead" :data="selectData" @update="handleUpdateCallback" />
  </div>
</template>

<script>

import CoSelect from './coSelect'
import { createAdd, updateArticle } from '@/api/village'
import { showNotice } from '@/utils/toolclass'

export default {
  components: {
    CoSelect
  },
  props: {
    data: {
      type: Object,
      default: () => ({})
    },
    isEdit: {
      type: Boolean
    }
  },
  data() {
    return {
      coSelectVisible: false,
      factorytable: [],
      selectData: {},
      tablehead: [
        {
          index: 1,
          prop: 'name',
          label: '地产品牌名称',
          sortable: false,
          fixed: true,
          isShow: true
        }
      ],
      params: {
        title: '地产品牌名称',
        placeholder: '请输入地产品牌名称...',
        location: 'center'
      }
    }
  },
  watch: {
    editFactory: {
      deep: true, // 深度监听
      handler(newVal, oldVal) {
        console.log(newVal, oldVal)
      }
    }
  },
  created() {},
  methods: {
    // 更新信息回调
    handleUpdateCallback(res) {
      console.log(res)
      if (res.code === '000000') {
        this.factorytable = res
        showNotice('success', '提示', '保存成功!')
        this.$emit('handleRefresh')
        this.$emit('handleCloseDialog')
      }
    },
    handleSelect() {
      this.coSelectVisible = true
    },
    handleUpdate() {
      updateArticle(this.data).then((res) => {
        if (res.code === '000000') {
          showNotice('success', '提示', '保存成功!')
          this.$emit('handleRefresh')
          this.$emit('handleCloseDialog')
        }
      })
    },
    handleSave() {
      if (this.data.name === '') {
        showNotice('error', '提示', '小区名称不能为空!')
      } else {
        this.handledata(this.data)
      }
    },
    handleCloseDialog() {
      console.log('子组件关闭')
      this.$emit('handleCloseDialog')
    },
    handleCloseDialogUpdate() {
      this.$emit('handleRefresh')
      this.$emit('handleCloseDialog')
    },
    handledata(obj) {
      createAdd(obj).then((res) => {
        if (res.code === '000000') {
          showNotice('success', '提示', '保存成功!')
          this.$emit('handleRefresh')
          this.$emit('handleCloseDialog')
        }
      })
    }
  }
}
</script>

<style rel='stylesheet/scss' lang='scss' scoped>
.produceFactoryDialog {
  width: 100%;
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  min-height: 450px;
  background-color: #f5f5f5;
  border-radius: 5px;
  .con {
    padding-top: 16px;
  }
  p {
    width: 100%;
    height: 0;
    text-align: center;
    margin: 6px 0 10px -65px;
  }
  .selBtn {
     right: 0;
     top: 0;
     height: 30px;
     line-height: 5px;
  }
  .panelWrap {
    height: 100%;
    position: absolute;
    width: 100%;
  }
  .con-row {
    padding: 10px 0 0 0;
    .inputLable {
      .name {
        width: 36%;
        text-align: right;
      }
      .input {
        margin-left: 3px;
      }
    }
    .textarea {
      width: 260px !important;
      border-style: solid;
      border-width: 1px;
      border-color: var(--inputbc);
      outline: none;
      height: 70px;
      border-radius: 3px;
    }
  }
  .footerBtn {
    .defaultbtn {
      width: 75px;
      height: 32px;
    }
  }
}
</style>
